//
//  GengDuoXueXiaoVC.m
//  ZhiBoYuan
//
//  Created by 白迪 on 2018/6/26.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "GengDuoXueXiaoVC.h"
#import "GengDuoXueXIaoCell.h"
#import "BranchCourseVC.h"

@interface GengDuoXueXiaoVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign)int page;

@end

@implementation GengDuoXueXiaoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"合作学校";
    
    _page = 1;
    
    [self initWithViews];
    
    [self downloadBranchCourseDatas];
}

- (void)downloadBranchCourseDatas{

    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                       @"page":[NSString stringWithFormat:@"%d",_page],
                       @"page_count":@"10"
                       };
    [SGHttpTool POST:Host_Append(hezuoxuexiao) parameter:parameter sucess:^(id responseObject) {

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        
        ZYLog(@"%@",responseObject);
        
        if (_page == 1) {
            [_dataArray removeAllObjects];
            for (NSDictionary *dicc in responseObject[@"data"][@"sub_banner_list"]) {
                [self.dataArray addObject:dicc];
            }
        }else{
            if ([responseObject[@"data"][@"sub_banner_list"] count] > 0) {
                for (NSDictionary *dicc in responseObject[@"data"][@"sub_banner_list"]) {
                    [self.dataArray addObject:dicc];
                }
            }
            
        }


        [self.tableView reloadData];

    } failure:^(NSError *error) {
        NSLog(@"%@",error);

    }];
    
}


#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifity = @"GengDuoXueXIaoCell";
    GengDuoXueXIaoCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[GengDuoXueXIaoCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifity];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    cell.nameLable.text = [_dataArray[indexPath.row] valueForKey:@"name"];
    [cell.imageVC sd_setImageWithURL:[_dataArray[indexPath.row] valueForKey:@"app_img_url"] placeholderImage:[UIImage imageNamed:@"0009"]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BranchCourseVC *branch = [BranchCourseVC new];
    branch.fenyuan_id = [_dataArray[indexPath.row] valueForKey:@"fenyuan_id"];
    branch.titleName = [_dataArray[indexPath.row] valueForKey:@"name"];
    [self.navigationController pushViewController:branch animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120 * KscreenHeight;
}

#pragma mark - 视图
- (void)initWithViews{
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
}

#pragma makr -- property
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}




- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *  tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, Screenheight- NAVIGATION_BAR_HEIGHT ) style:UITableViewStylePlain];
        tableView.backgroundColor = [UIColor clearColor];
        tableView.delegate        = self;
        tableView.dataSource      = self;
        //        tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        tableView.estimatedRowHeight = 44.0f;//推测高度，必须有，可以随便写多少
        
        tableView.rowHeight = UITableViewAutomaticDimension;//iOS8之后默认就是这个值，可以省略
        
        //        self.extendedLayoutIncludesOpaqueBars = YES;
        
        if (@available(iOS 11.0, *)) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
        tableView.scrollIndicatorInsets = _tableView.contentInset;
        
        [tableView registerClass:[GengDuoXueXIaoCell class] forCellReuseIdentifier:@"GengDuoXueXIaoCell"];
        
        [tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        __weak __typeof(&*self)weakSelf = self;
        
        //         下拉刷新
        tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            _page = 1;
            
            [self downloadBranchCourseDatas];
            
            [weakSelf.tableView.mj_header endRefreshing];
        }];
        
        // 上拉加载
        tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            _page = _page + 1;
            
            [self downloadBranchCourseDatas];
            
        }];
        _tableView = tableView;
        
    }
    return _tableView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
